package com.jasonchoi.eurekaclient2.config;


import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Predicate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

/**
 * Swagger2 配置类
 * 使用 swagger-bootstrap-ui 文档地址： https://doc.xiaominfo.com/
 * @Author: JasonChoi
 * @Date: 2019/12/24 16:31
 */
@EnableKnife4j
@EnableSwagger2
@Configuration
@Import(BeanValidatorPluginsConfiguration.class)
public class Swagger2Config {

    private String basePath = "";

    @Value("${spring.application.name}")
    private String applicationName;

    public String getGroupFullName(String groupName){
        return String.format("%s__%s",applicationName,groupName);
    }


    @Bean
    public Docket helloRestApi() {
        Predicate<String> pathRule = or(
                regex(basePath + "/hello/.*")
        );

        return getBaseDocket(pathRule).groupName(getGroupFullName("hello-group相关接口"));
    }

    @Bean
    public Docket byeRestApi() {
        Predicate<String> pathRule = or(
            regex(basePath + "/bye/.*")
        );
        return getBaseDocket(pathRule).groupName(getGroupFullName("bye-group相关接口"));
    }

    /**
     * 构建一个基础的Docket
     * @return
     * @param pathRule 过滤的路径规则
     */
    private Docket getBaseDocket(Predicate<String> pathRule) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()).select()
                .paths(pathRule).build()
                //隐藏默认提供的响应异常描述
                .useDefaultResponseMessages(false)
                ;
    }

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.jasonchoi.eurekaclient2.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("EUREKA-CLIENT2")
                .description("客户端2")
                //这里配置的是服务网站
                .termsOfServiceUrl("http://localhost")
                // 三个参数依次是姓名，个人网站，邮箱
                .contact(new Contact("JasonChoi", "http://localhost", "xxx@xxx.com.cn"))
                .version("1.0")
                .build();
    }
}